package com.shop2cn.app.liveseller.testcase;

import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.app.liveseller.parameter.*;
import com.shop2cn.app.liveseller.service.*;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.*;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.livesellerShop2cn.LiveRoom;
import com.ymttest.database.sqlwapper.livesellerShop2cn.LiveRoomShop2cnWapper;
import com.ymttest.utils.YMTDateUtil;
/**
*买手关闭直播/结束直播
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CloseSellerLive{
	private static CloseSellerLiveBean closesellerliveBean;
	private static CloseSellerLiveCall closesellerliveCall;
	private static LiveRoomShop2cnWapper livesWapper ;
	private static int sellerId ;
	private static String token ;
	private static int csId;
	private static String cstoken;
	@BeforeClass
	public static void setUp() throws Exception {
		Logger.createResultFile("买手关闭直播/结束直播");
		sellerId = GodmchManageCallService.CreateSellerMch();
		token=AuthCallServiceV2.createToken(sellerId, "app");
		csId = GodmchManageCallService.bindClientService(
				sellerId, 1);
		cstoken = AuthCallServiceV2.createToken(csId, "app");
	}

	@Before
	public void caseUp() {
		closesellerliveBean=new CloseSellerLiveBean();
		closesellerliveCall=new CloseSellerLiveCall();
		livesWapper=new LiveRoomShop2cnWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(closesellerliveCall.getOpurl());
		livesWapper=null;
	}

	@After
	public void caseDown() {
		
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CloseSellerLive_001() {
		Logger.start(true,"买手关闭直播成功,直播商品删除");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProductV2(sellerId, "autotest",
								false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			LiveRoomShop2cnCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), Arrays.asList(pid));
			
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setCloseType("1");
			closesellerliveCall.setData(closesellerliveBean);
			String head="accesstoken="+token;
			closesellerliveCall.addHeader("ymt-pars", head);
			closesellerliveCall.callService();			
			
			createlive=livesWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(8, createlive.getLiveStatus() ,"验证LiveStatus");
			 LiveProductListCall liveProductListCall = LiveRoomShop2cnCallService.GetLiveProductList(createlive.getId(), createlive.getUserId(), 2);
			Logger.verifyEquals(0, liveProductListCall.getSuspendCount(), "验证关闭直播时候商品删除SuspendCount");
			Logger.verifyEquals(0, liveProductListCall.getSoldOutCount(), "验证关闭直播时候商品删除SoldOutCount");
			Logger.verifyEquals(0, liveProductListCall.getOnsaleCount(), "验证关闭直播时候商品删除OnsaleCount");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_002() {
		Logger.start(true,"买手结束直播成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProductV2(sellerId, "autotest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			LiveRoomShop2cnCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), Arrays.asList(pid));
			
			LiveRoomShop2cnCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			
			livesWapper.updateLiveRoomTime(YMTDateUtil.getBeforeOrNextMunite(-100), YMTDateUtil.getBeforeOrNextMunite(-50),YMTDateUtil.getBeforeOrNextMunite(-50), createlive.getId());
			
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("2");
			closesellerliveCall.setData(closesellerliveBean);
			String head="accesstoken="+token;
			closesellerliveCall.addHeader("ymt-pars", head);
			closesellerliveCall.callService();
			
			createlive=livesWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(9, createlive.getLiveStatus() ,"验证LiveStatus");
			//1215需求不下架
			LiveProductListCall liveProductListCall = LiveRoomShop2cnCallService.GetLiveProductList(createlive.getId(), createlive.getUserId(), 2);
			Logger.verifyEquals(0, liveProductListCall.getSuspendCount(), "验证关闭直播时候商品暂停销售SuspendCount");
			Logger.verifyEquals(0, liveProductListCall.getSoldOutCount(), "验证关闭直播时候商品暂停销售SoldOutCount");
			Logger.verifyEquals(1, liveProductListCall.getOnsaleCount(), "验证关闭直播时候商品暂停销售OnsaleCount");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_003() {
		Logger.start(true,"不输入liveid失败");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
//			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("1");
			closesellerliveCall.setData(closesellerliveBean);
			String head="accesstoken="+token;
			closesellerliveCall.addHeader("ymt-pars", head);
			closesellerliveCall.callService();			
			Logger.verifyEquals("400", closesellerliveCall.getString("code"), "验证Code");
			Logger.verifyIsContains("直播Id参数错误", closesellerliveCall.getString("msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_004() {
		Logger.start(true,"不输入UserId失败");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			closesellerliveBean.setLiveId(createlive.getId());
//			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("1");
			closesellerliveCall.setData(closesellerliveBean);
			closesellerliveCall.callService();			
			Logger.verifyEquals("400", closesellerliveCall.getString("code"), "验证Code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_005() {
		Logger.start(true,"输入liveid和userid不匹配失败");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setCloseType("1");
			closesellerliveCall.setData(closesellerliveBean);
			int sellerId1= GodmchManageCallService.CreateSellerMch();
			String token1=AuthCallServiceV2.createToken(sellerId1, "app");
			String head="accesstoken="+token1;
			closesellerliveCall.addHeader("ymt-pars", head);
			closesellerliveCall.callService();			
			Logger.verifyEquals("201", closesellerliveCall.getString("code"), "验证Code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_006() {
		Logger.start(true,"不输入CloseType失败");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
//			closesellerliveBean.setCloseType("1");
			closesellerliveCall.setData(closesellerliveBean);
			String head="accesstoken="+token;
			closesellerliveCall.addHeader("ymt-pars", head);
			closesellerliveCall.callService();			
			Logger.verifyEquals("400", closesellerliveCall.getString("code"), "验证Code");
			Logger.verifyIsContains("不能为空", closesellerliveCall.getString("msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_007() {
		Logger.start(true,"买手未开始结束直播提示直播未开始，无法结束");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("2");
			closesellerliveCall.setData(closesellerliveBean);
			String head="accesstoken="+token;
			closesellerliveCall.addHeader("ymt-pars", head);
			closesellerliveCall.callService();
			Logger.verifyEquals("201", closesellerliveCall.getString("code"), "验证Code");
			Logger.verifyIsContains("直播未开始，无法结束", closesellerliveCall.getString("msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_008() {
		Logger.start(true,"买手已经开始关闭直播失败,应该结束直播");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			//操作数据库更新开始时间 这里可能有时间差
			LiveRoomShop2cnCallService.UpdateLiveTimeToStart(createlive.getId());
			LiveRoomShop2cnCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("1");
			closesellerliveCall.setData(closesellerliveBean);
			String head="accesstoken="+token;
			closesellerliveCall.addHeader("ymt-pars", head);
			closesellerliveCall.callService();
			Logger.verifyEquals("201", closesellerliveCall.getString("code"), "验证Code");
			Logger.verifyIsContains("", closesellerliveCall.getString("msg"), "验证Msg");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CloseSellerLive_009() {
		Logger.start(true,"重复关闭直播,数据不会异常");
		try {
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("1");
			closesellerliveCall.setData(closesellerliveBean);
			String head="accesstoken="+token;
			closesellerliveCall.addHeader("ymt-pars", head);
			closesellerliveCall.callService();
			closesellerliveCall.callService();	
			Logger.verifyEquals("201", closesellerliveCall.getString("code"), "验证Code");
			Logger.verifyIsContains("已关闭", closesellerliveCall.getString("msg"), "验证Msg");
			createlive=livesWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(8, createlive.getLiveStatus() ,"验证LiveStatus");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CloseSellerLive_010() {
		Logger.start(true,"未到时间买手结束直播成功");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProductV2(sellerId, "autotest",
					false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			LiveRoomShop2cnCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), Arrays.asList(pid));
			
			LiveRoomShop2cnCallService.SellerLiveOnline(createlive.getId(), createlive.getUserId());
			
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setUserId(createlive.getUserId());
			closesellerliveBean.setCloseType("2");
			closesellerliveCall.setData(closesellerliveBean);
			String head="accesstoken="+token;
			closesellerliveCall.addHeader("ymt-pars", head);
			closesellerliveCall.callService();
			createlive=livesWapper.selectLiveRoomById(createlive.getId());
			Logger.verifyEquals(9, createlive.getLiveStatus() ,"验证LiveStatus");

//			Logger.verifyIsContains("未到", closesellerliveCall.getString("msg"), "验证Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CloseSellerLive_011() {
		Logger.start(true,"客服买手关闭直播失败");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProductV2(sellerId, "autotest",
								false);
			String pid = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			Logger.comment("创建商品：" + pid);
			LiveRoom createlive = LiveRoomShop2cnCallService.CreateSellerLive(LiveRoomShop2cnCallService.initCreatesellerliveBean(),sellerId);
			LiveRoomShop2cnCallService.AddProductsToLive(createlive.getId(), createlive.getUserId(), Arrays.asList(pid));
			
			closesellerliveBean.setLiveId(createlive.getId());
			closesellerliveBean.setCloseType("1");
			closesellerliveCall.setData(closesellerliveBean);
			String head="accesstoken="+cstoken;
			closesellerliveCall.addHeader("ymt-pars", head);
			closesellerliveCall.callService();			
			Logger.verifyEquals("400", closesellerliveCall.getString("code"), "验证Code");
			Logger.verifyIsContains("客服不能", closesellerliveCall.getString("msg"), "验证Msg");
//			createlive=livesWapper.selectLiveRoomById(createlive.getId());
//			Logger.verifyEquals(8, createlive.getLiveStatus() ,"验证LiveStatus");
//			 LiveProductListCall liveProductListCall = LiveRoomShop2cnCallService.GetLiveProductList(createlive.getId(), createlive.getUserId(), 2);
//			Logger.verifyEquals(0, liveProductListCall.getSuspendCount(), "验证关闭直播时候商品删除SuspendCount");
//			Logger.verifyEquals(0, liveProductListCall.getSoldOutCount(), "验证关闭直播时候商品删除SoldOutCount");
//			Logger.verifyEquals(0, liveProductListCall.getOnsaleCount(), "验证关闭直播时候商品删除OnsaleCount");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}